package org.apoiolivre.ui.android;

import java.util.Hashtable;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

public class MovimentoDataListAdapter extends BaseAdapter {

	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// data members
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	/** holds all the weather data */
	private Hashtable<String, Hashtable<String, String>> _data = new Hashtable<String, Hashtable<String, String>>();

	/** holds the currently selected position */
	private int _selectedIndex;
	private Activity _context;

	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// constructor
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	/**
	 * create the model-view object that will control the listview
	 *
	 * @param context  activity that creates this thing
	 * @param listview bind to this listview
	 */
	public MovimentoDataListAdapter(final Activity context, ListView listview) {

	  // save the activity/context ref
	  _context = context;

	  // bind this model (and cell renderer) to the listview
	  listview.setAdapter(this);

	  // load some data into the model
	  {
	    String zip = "12345";
	    Hashtable<String, String> wd = new Hashtable<String, String>();
	    wd.put("temperature","30F");
	    wd.put("humidity","50%");
	    wd.put("icon","12");
	    _data.put(zip, wd);
	  }

	  Log.i(getClass().getSimpleName(), "loading data set, creating list model, and binding to listview");

	}
	
	
	
	
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// handle list selection
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

	/**
	 * called when item in listview is selected... fires a model changed event...
	 *
	 * @param index index of item selected in listview. if -1 then it's unselected.
	 */
	public void setSelected(int index) {

	  if (index == -1) {
	    // unselected
	  }
	  else {
	    // selected index...
	  }

	  _selectedIndex = index;

	  // notify the model that the data has changed, need to update the view
	  notifyDataSetChanged();

	  Log.i(getClass().getSimpleName(),
	        "updating _selectionIndex with index and firing model-change-event: index=" + index);

	}

	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	// implement ListAdapter - how big is the underlying list data, and how to iterate it...
	// the underlying data is a Map of Maps, so this really reflects the keyset of the enclosing Map...
	//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
	/** returns all the items in the {@link #_data} table */
	public int getCount() {
	  return _data.size();
	}

	/** returns the key for the table, not the value (which is another table) */
	public Object getItem(int i) {
	  Object retval = _data.keySet().toArray()[i];
	  Log.i(getClass().getSimpleName(), "getItem(" + i + ") = " + retval);
	  return retval;
	}

	/** returns the unique id for the given index, which is just the index */
	public long getItemId(int i) {
	  return i;
	}




	public View getView(int index, View cellRenderer, ViewGroup parent) {
		 CellRendererView cellRendererView = null;

		  if (cellRenderer == null) {
		    // create the cell renderer
		    Log.i(getClass().getSimpleName(), "creating a CellRendererView object");
		    cellRendererView = new CellRendererView();
		  }
		  else {
		    cellRendererView = (CellRendererView) cellRenderer;
		  }

		  // update the cell renderer, and handle selection state
		  cellRendererView.display(index,
		                           _selectedIndex == index);

		  return cellRendererView;

	}
}